//
//  UserCenterHomeViewController.m
//  much
//
//  Created by weijunjie on 2019/1/4.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "UserCenterHomeViewController.h"
#import "UserCenterHomeTableHeaderView.h"
#import "UserCenterHomeTableViewCell.h"
#import "SettingUserMessageViewController.h"
#import "GoodsCollectionViewController.h"
#import "ArticleCollectionViewController.h"
#import "OrderPageViewController.h"

@interface UserCenterHomeViewController () <UITableViewDelegate, UITableViewDataSource, DelegateSelectedCollectionViewDelegate>

@property (nonatomic, copy) UITableView *tableView;
@property (nonatomic, copy) UserCenterHomeTableHeaderView *headerView;
/** 组数组 */
@property (nonatomic, strong) NSMutableArray *sectionMtbArray;
/** 普通数组 */
@property (nonatomic, strong) NSMutableArray *myNormalMtbArray;
/** 订单数组 */
@property (nonatomic, strong) NSMutableArray *myOrderMtbArray;
/** 我的助理数组 */
@property (nonatomic, strong) NSMutableArray *myAssistantMtbArray;

@end

@implementation UserCenterHomeViewController

- (void)_reLayout {
    self.navigationView.title = @"我的";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self judgeSection];
}


#pragma mark - method
/** 组装数据 */
- (void)judgeSection {
    [MuchUserRequestManager getUserBaseMessage:^(UserBaseInfoModel *model) {
        UserBaseInfoModel *userBaseInfoModel = USER.userBaseInfo.infoModel;
        userBaseInfoModel = model;
        USER.userBaseInfo.infoModel = userBaseInfoModel;
        
        self.headerView.uploadValue = YES;
        [self.tableView reloadData];
    } failureBlock:^(NSString *error) {
    }];
}
/** 修改个人信息 */
- (void)pushToSetUserMassage {
    SettingUserMessageViewController *vc = [[SettingUserMessageViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
/** 商品收藏 */
- (void)pushToShopCollection {
    GoodsCollectionViewController *vc = [[GoodsCollectionViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
/** 我的评价 */
- (void)pushMyEvaluation {
    
}
/** 文章收藏 */
- (void)pushArticleCollection {
    ArticleCollectionViewController * vc = [[ArticleCollectionViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
/** 关注作者 */
- (void)pushPayAttentionToAuthor {
    
}
/** 订单 */
- (void)pushToOrder:(UserCenterHomeTableViewCellType)tag {
    NSInteger index = 0;
    switch (tag) {
        case UCHTVCTypeForThePayment:
            index = 1;
            break;
        case UCHTVCTypeDropShipping:
            index = 2;
            break;
        case UCHTVCTypeTheCollectingCargo:
            index = 3;
            break;
        case UCHTVCTypeCompleted:
            index = 0;
            break;
        case UCHTVCTypeRefundAfterSale:
            index = 5;
            break;
        default:
            break;
    }
    if (index < 4) {
        OrderPageViewController *vc = [[OrderPageViewController alloc] init];
        vc.moveIndex = index;
        [self.navigationController pushViewController:vc animated:YES];
    }
//    else if (index == 4) {
//        AppraiseListBaseViewController *vc = [[AppraiseListBaseViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
//    } else {
//        NewStoreNewRefundListBaseViewController *vc = [[NewStoreNewRefundListBaseViewController alloc] init];
//        [self.navigationController pushViewController:vc animated:YES];
//    }
}
/** 退款/售后 */
- (void)pushToRefundAfterSale {
    
}
/** 营销名片 */
- (void)pushToMarketingBusinessCard {
    
}


#pragma mark - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionMtbArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UserCenterHomeTableViewCell *cell = [UserCenterHomeTableViewCell initTableView:tableView];
    return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    UserCenterHomeTableViewCell *userCenterHomeTableViewCell = (UserCenterHomeTableViewCell *)cell;
    userCenterHomeTableViewCell.delegateCollectionViewDelegate = self;
    userCenterHomeTableViewCell.dic = self.sectionMtbArray[indexPath.section];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.sectionMtbArray[indexPath.section][SectionTag] integerValue] == UCHVSectionTypeOrder) {
        [self pushToOrder:0];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat normalHeight = 44.f;
    switch ([self.sectionMtbArray[indexPath.section][SectionTag] integerValue]) {
        case UCHVSectionTypeNormal: {
//            normalHeight += ceil([self.myNormalMtbArray count] /4.0) *21;
            normalHeight += ceil([self.myNormalMtbArray count] /4.0) *31;
            return normalHeight;
            break;
        }
        case UCHVSectionTypeOrder: {
            normalHeight += ceil([self.myOrderMtbArray count] /5.0) *75;
            return normalHeight;
            break;
        }
        case UCHVSectionTypeAssistant: {
            normalHeight += ceil([self.myAssistantMtbArray count] /5.0) *75;
            return normalHeight;
            break;
        }
        default:
            break;
    }
    return normalHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001f;
}


#pragma mark - <DelegateSelectedCollectionViewDelegate>
- (void)clickCollectionRow:(UserCenterHomeTableViewCellType)cellectionRow {
    if (USER.isLogin) {
        switch (cellectionRow) {
            case UCHTVCTypeSetting:
                [self pushToSetUserMassage];
                break;
            case UCHTVCTypeShopCollection:
                [self pushToShopCollection];
                break;
            case UCHTVCTypeMyEvaluation:
                [self pushMyEvaluation];
                break;
            case UCHTVCTypeArticleCollection:
                [self pushArticleCollection];
                break;
            case UCHTVCTypePayAttentionToAuthor:
                [self pushPayAttentionToAuthor];
                break;
            case UCHTVCTypeForThePayment:
            case UCHTVCTypeDropShipping:
            case UCHTVCTypeTheCollectingCargo:
            case UCHTVCTypeCompleted:
                [self pushToOrder:cellectionRow];
                break;
            case UCHTVCTypeRefundAfterSale:
                [self pushToRefundAfterSale];
                break;
            case UCHTVCTypeMarketingBusinessCard:
                [self pushToMarketingBusinessCard];
                break;
            default:
                break;
        }
    } else {
        [USER logout:^(id  _Nonnull success) {}];
    }
}


#pragma mark - 遍历数组处理字典
/** 遍历数组移除字典 */
- (void)traverseToolArrayForRemoveDic:(NSInteger)key sectionType:(UserCenterHomeViewSectionType)sectionType {
    switch (sectionType) {
        case UCHVSectionTypeNormal: {
            [self.myNormalMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    [self.myNormalMtbArray removeObjectAtIndex:idx];
                }
            }];
            break;
        }
        case UCHVSectionTypeOrder: {
            [self.myOrderMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    [self.myOrderMtbArray removeObjectAtIndex:idx];
                }
            }];
            break;
        }
        case UCHVSectionTypeAssistant: {
            [self.myAssistantMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    [self.myAssistantMtbArray removeObjectAtIndex:idx];
                }
            }];
        }
        default:
            break;
    }
    [self reloadTableViewDataSource:sectionType];
}
/** 遍历数组添加字典 */
- (void)traverseToolArrayForAddDic:(NSInteger)key sectionType:(UserCenterHomeViewSectionType)sectionType {
    switch (sectionType) {
        case UCHVSectionTypeNormal:
            break;
        case UCHVSectionTypeOrder:
            break;
        case UCHVSectionTypeAssistant: {
            switch (key) {
                case UCHTVCTypeMarketingBusinessCard: {
                    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"营销名片", CellTag:@(UCHTVCTypeMarketingBusinessCard), CellImageName:@"user_home_marketingBusinessCard", CellCount:@"0", CellHasMessage:@(NO)}];
                    //                    [self.myAssistantMtbArray insertObject:dic atIndex:[self traverseTo0lArrayForIndex:UCHTVCTypeMarketingBusinessCard sectionType:sectionType]];
                    [self.myAssistantMtbArray insertObject:dic atIndex:0];
                    break;
                }
                default:
                    break;
            }
        }
        default:
            break;
    }
    [self reloadTableViewDataSource:sectionType];
}
/** 遍历数组加载字典 */
- (void)traverseToolArrayForSetDic:(NSInteger)key keyTitle:(NSString *)keyTitle keyValue:(NSString *)keyValue sectionType:(UserCenterHomeViewSectionType)sectionType {
    switch (sectionType) {
        case UCHVSectionTypeNormal: {
            [self traverseToolArrayForRemoveDic:key sectionType:sectionType];
            [self traverseToolArrayForAddDic:key sectionType:sectionType];
            [self.myNormalMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    [obj setObject:keyValue forKey:keyTitle];
                }
            }];
            break;
        }
        case UCHVSectionTypeOrder: {
            [self traverseToolArrayForRemoveDic:key sectionType:sectionType];
            [self traverseToolArrayForAddDic:key sectionType:sectionType];
            [self.myOrderMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    [obj setObject:keyValue forKey:keyTitle];
                }
            }];
            break;
        }
        case UCHVSectionTypeAssistant: {
            [self traverseToolArrayForRemoveDic:key sectionType:sectionType];
            [self traverseToolArrayForAddDic:key sectionType:sectionType];
            [self.myAssistantMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    [obj setObject:keyValue forKey:keyTitle];
                }
            }];
            break;
        }
        default:
            break;
    }
    
}
/** 遍历数组获取下标 */
- (NSInteger)traverseTo0lArrayForIndex:(NSInteger)key sectionType:(UserCenterHomeViewSectionType)sectionType {
    __block NSInteger index = 0;
    switch (sectionType) {
        case UCHVSectionTypeNormal: {
            [self.myNormalMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    index = idx;
                    *stop = YES;
                }
            }];
            break;
        }
        case UCHVSectionTypeOrder: {
            [self.myOrderMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    index = idx;
                    *stop = YES;
                }
            }];
            break;
        }
        case UCHVSectionTypeAssistant: {
            [self.myAssistantMtbArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([[obj objectForKey:CellTag] integerValue] == key) {
                    index = idx;
                    *stop = YES;
                }
            }];
        }
        default:
            break;
    }
    return index +1;
}
/** 刷新数组 */
- (void)reloadTableViewDataSource:(UserCenterHomeViewSectionType)sectionType {
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:(NSDictionary *)self.sectionMtbArray[sectionType]];
    switch (sectionType) {
        case UCHVSectionTypeNormal: {
            [dic setObject:self.myNormalMtbArray forKey:SectionCellArray];
            break;
        }
        case UCHVSectionTypeOrder: {
            [dic setObject:self.myOrderMtbArray forKey:SectionCellArray];
            break;
        }
        case UCHVSectionTypeAssistant: {
            [dic setObject:self.myAssistantMtbArray forKey:SectionCellArray];
            break;
        }
        default:
            break;
    }
    [self.sectionMtbArray setObject:dic atIndexedSubscript:sectionType];
    [self.tableView reloadData];
}


#pragma mark - 懒加载
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color244;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellAccessoryNone;
        _tableView.tableHeaderView = self.headerView;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.navigationView.mas_bottom);
            make.left.and.right.and.bottom.equalTo(self.view);
        }];
    }
    return _tableView;
}
- (UserCenterHomeTableHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[UserCenterHomeTableHeaderView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 167)];
        
        __weakConfigure
        _headerView.userCenterHomeTableHVBlock = ^{
            [__weakSelf clickCollectionRow:UCHTVCTypeSetting];
        };
    }
    return _headerView;
}


#pragma mark - 初始化基础数据
- (NSMutableArray *)sectionMtbArray {
    if (!_sectionMtbArray) {
        _sectionMtbArray = [[NSMutableArray alloc] init];
        NSDictionary *dic;
//        dic = [NSMutableDictionary dictionaryWithDictionary:@{SectionTitle:@"", SectionTag:@(UCHVSectionTypeNormal), SectionShowRight:@(NO), SectionCellArray:self.myNormalMtbArray}];
//        [_sectionMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{SectionTitle:@"我的订单", SectionTag:@(UCHVSectionTypeOrder), SectionShowRight:@(YES), SectionCellArray:self.myOrderMtbArray}];
        [_sectionMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{SectionTitle:@"我的助理", SectionTag:@(UCHVSectionTypeAssistant), SectionShowRight:@(NO), SectionCellArray:self.myAssistantMtbArray}];
        [_sectionMtbArray addObject:dic];
    }
    return _sectionMtbArray;
}
- (NSMutableArray *)myNormalMtbArray {
    if (!_myNormalMtbArray) {
        _myNormalMtbArray = [[NSMutableArray alloc] init];
        NSMutableDictionary *dic;
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"商品收藏", CellTag:@(UCHTVCTypeShopCollection), CellImageName:@"user_home_shopCollection", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myNormalMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"我的评价", CellTag:@(UCHTVCTypeMyEvaluation), CellImageName:@"user_home_myEvaluation", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myNormalMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"文章收藏", CellTag:@(UCHTVCTypeArticleCollection), CellImageName:@"user_home_articleCollection", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myNormalMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"关注作者", CellTag:@(UCHTVCTypePayAttentionToAuthor), CellImageName:@"user_home_payAttentionToAuthor", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myNormalMtbArray addObject:dic];
    }
    return _myNormalMtbArray;
}
- (NSMutableArray *)myOrderMtbArray {
    if (!_myOrderMtbArray) {
        _myOrderMtbArray = [[NSMutableArray alloc] init];
        NSMutableDictionary *dic;
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"待付款", CellTag:@(UCHTVCTypeForThePayment), CellImageName:@"user_home_forThePayment", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myOrderMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"待发货", CellTag:@(UCHTVCTypeDropShipping), CellImageName:@"user_home_dropShipping", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myOrderMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"待收货", CellTag:@(UCHTVCTypeTheCollectingCargo), CellImageName:@"user_home_theCollectingCargo", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myOrderMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"已完成", CellTag:@(UCHTVCTypeCompleted), CellImageName:@"user_home_completed", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myOrderMtbArray addObject:dic];
        
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"退款/售后", CellTag:@(UCHTVCTypeRefundAfterSale), CellImageName:@"user_home_refundAfterSale", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myOrderMtbArray addObject:dic];
    }
    return _myOrderMtbArray;
}
- (NSMutableArray *)myAssistantMtbArray {
    if (!_myAssistantMtbArray) {
        _myAssistantMtbArray = [[NSMutableArray alloc] init];
        NSMutableDictionary *dic;
        dic = [NSMutableDictionary dictionaryWithDictionary:@{CellTitle:@"营销名片", CellTag:@(UCHTVCTypeMarketingBusinessCard), CellImageName:@"user_home_marketingBusinessCard", CellCount:@"0", CellHasMessage:@(NO)}];
        [_myAssistantMtbArray addObject:dic];
    }
    return _myAssistantMtbArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

@end
